(function($){
    var client = new com.im.common.ajax();
    var loading = false;

    var methods = {
        init: function(options) {
            var o = $.extend({
                elmIdSel : "unitSel",//需要展示的位置下拉框
                unitType : "",//常量编码
                defaultContent : true ,//组织下拉框默认显示文本，true全部 false请选择
                callbackFunc: function(paramValValue,paramValName) {
                }
            }, options || {});

            var self = this;

            onInit(o);

            return this;
        },
        treeOnClickInner : function(self,opt){
            //加载数据
            return o.treeOnClick('1','test');
        }

    };

    function onInit(opt) {
        var param = {"forOrgList": ""};
        if(opt.unitType != undefined && opt.unitType != ""){
            param.orgType = opt.unitType;
        }
       //加载数据
        var result = client.callAsJson("userService/queryOrgarizationsByCond", param );
        if (result.success) {
            //规划类型列表添加元素
            if(opt.defaultContent){
                $("#"+opt.elmIdSel).append('<option value="">全部</option>');
            }else{
                if(opt.org = ''){
                    $("#"+opt.elmIdSel).append('<option value="">请选择</option>');
                }else if(opt.org = '10'){
                    $("#"+opt.elmIdSel).append('<option value="">请选择建设单位</option>');
                }else if(opt.org = '20'){
                    $("#"+opt.elmIdSel).append('<option value="">请选择监理单位</option>');
                }else if(opt.org = '30'){
                    $("#"+opt.elmIdSel).append('<option value="">请选择承建单位</option>');
                }else if(opt.org = '40'){
                    $("#"+opt.elmIdSel).append('<option value="">请选择代理机构</option>');
                }
            }
            if(result.result != undefined){
                $.each(result.result, function (i, item) {
                    $("#"+opt.elmIdSel).append('<option value="'+item.orgId+'">'+item.orgName+'</option>');
                });
            }
            $('.select2').select2();
        }else{
            toastr.error('服务器忙，常量查询失败！'+result.resultMsg);
        }

        //初始化事件
        $("#"+opt.elmIdSel).change(function () {
            _returnFunc(opt);
        });

    };

    function _returnFunc(opt){
        var objvalue = $("#"+opt.elmIdSel).val();
        var objname = $("#"+opt.elmIdSel).find("option:selected").text();
        return opt.callbackFunc(objvalue,objname);
    };
    
    $.fn.unitselCommpent = function(method) {

        if (methods[method] && method.charAt(0) != '_') {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.pagination');
        }
    };

})(jQuery);